<template>
  <div>
    <el-table  class="message-menue" :data="messageList" height="500" style="width: 100%">
      <el-table-column prop="mid" label="ID" min-width="10px" width="auto"></el-table-column>
      <el-table-column prop="fromUserId" label="用户ID" min-width="10px" width="auto"></el-table-column>
      <el-table-column prop="sessionId" label="唯一标识" min-width="10px" width="auto"></el-table-column>
      <el-table-column prop="content" label="提问" min-width="20px" width="auto">
        <template  slot-scope="scope">
          <div @click="openContent" class="reply-cell" ref="replyCell" :data-content="scope.row.content">
            {{ scope.row.content }}
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="when" label="时间(↓)" min-width="20px" width="auto"></el-table-column>
      <el-table-column prop="ipAddress" label="IP" min-width="10px" width="auto"></el-table-column>
      <el-table-column  prop="response" label="回复(点击查看详情)" min-width="30px" width="auto" >
        <template  slot-scope="scope">
          <div @click="openResponse" class="reply-cell" ref="replyCell" :data-reply="scope.row.response">
            {{ scope.row.response }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="entity" label="实体" min-width="10px" width="auto"></el-table-column>
      <el-table-column prop="good" label="最佳" min-width="10px" width="auto">
        <template  slot-scope="scope">
          <span style="font-size: 14px" v-if="scope.row.good === 0">否</span>
          <span style="font-size: 14px;font-weight: bold;color: #54bf93" v-else>是</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="openEditDialog(scope.row)">修改</el-button>
        </template>
      </el-table-column>



    </el-table>
    <el-button class="goodRes" @click="getMessageListByGood(currentPage,pageSize)">最佳回答</el-button>
    <!-- 编辑弹框 -->
    <el-dialog :visible="editDialogVisible" title="编辑对话" @close="closeEditDialog">
      <el-form :model="editFormData" label-width="80px">
        <el-form-item label="ID">
          <el-input v-model="editFormData.mid" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="用户ID">
          <el-input v-model="editFormData.fromUserId" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="唯一表示">
          <el-input v-model="editFormData.sessionId" :disabled="true" ></el-input>
        </el-form-item>
        <el-form-item label="提问">
          <el-input v-model="editFormData.content"></el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-input v-model="editFormData.when"></el-input>
        </el-form-item>
        <el-form-item label="IP">
          <el-input v-model="editFormData.ipAddress"></el-input>
        </el-form-item>
        <el-form-item label="回复">
          <el-input type="textarea"
                    :rows="inputRows"
                    :style="inputStyle"
                    @blur="recoverInput"
                    @focus="expandInput"
                    v-model="editFormData.response"></el-input>
        </el-form-item>
        <el-form-item label="实体">
          <el-input v-model="editFormData.entity"></el-input>
        </el-form-item>
        <el-form-item label="最佳">
          <el-input v-model="editFormData.good"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeEditDialog">取消</el-button>
        <el-button type="primary" @click="saveEditData">保存</el-button>
      </div>
    </el-dialog>
    <!-- 分页组件 -->
    <el-pagination class="page"
                   background
                   layout="prev, pager, next"
                   :total="500"
                   @current-change="handlePageChange">
    </el-pagination>
  </div>
</template>

<script>
import {Loading} from "element-ui";

export default {
  name: "Message",
  data() {
    return {
      messageList: [],
      currentPage: 1,  // 当前页码
      pageSize: 10,  // 每页显示的条数
      totalItems: 500,  // 总条数
      replyText:"",
      editDialogVisible: false, // 编辑弹框是否显示
      editFormData: {}, // 编辑表单数据
      replyContent: '',
      inputRows: 1,
      inputStyle: '',
    };
  },
  created() {
    let loading = Loading.service({fullscreen: true});
    this.getMessageList(this.currentPage, this.pageSize)
    loading.close()
  },
  methods: {
    expandInput() {
      this.inputRows = 5; // 设置输入框的行数，可以根据需要进行调整
    },
    recoverInput(){
      this.inputRows = 1; // 设置输入框的行数，可以根据需要进行调整
    },
    openEditDialog(row) {
      // 打开编辑弹框并回显数据
      this.editFormData = { ...row };

      this.editDialogVisible = true;
    },
    closeEditDialog() {
      // 关闭编辑弹框
      this.editDialogVisible = false;
    },
    saveEditData() {
      this.$store.dispatch('updateMessageInfo',this.editFormData).then((res) => {
        location.reload()
        this.$message({
          message: res,
          type: 'success'
        });
      })
      console.log(this.editFormData)
      this.editDialogVisible = false;
    },
    handlePageChange(currentPage) {
      this.currentPage = currentPage; // 更新当前页码
      this.getMessageList(currentPage, this.pageSize); // 加载对应页码的对话数据
    },
    getMessageList(current, limit) {
      this.$store.dispatch('getMessageList', {current, limit}).then((res) => {
        this.messageList = res.messageList.records;
        this.totalItems = res.total
      })
    },
    getMessageListByGood(current, limit) {
      this.$store.dispatch('getMessageListByGood', {current, limit}).then((res) => {
        this.messageList = res.messageList.records;
        this.totalItems = res.total
      })
    },
    openResponse(event) {
      let str = event.target.dataset.reply;
      // let str2 = str.replace("\n", "<br/>");
      // this.replyText = str.replace("\\n", "<br/>");
      // console.log(str.replace(" \n","<br/>"))
      this.$alert(`<div style="height: 400px; overflow-y: auto;">${str}</div>`, '回复展示', {
        dangerouslyUseHTMLString: true,
        confirmButtonText: '确定',
      });
    },
    openContent(event) {
      let str = event.target.dataset.content;
      // this.replyText = str.replace(/ \\n/g, "<br/>");
      // console.log(str.replace(" \n","<br/>"))
      this.$alert(`<div style="height: 200px; overflow-y: auto;">${str}</div>`, '请求展示', {
        dangerouslyUseHTMLString: true,
        confirmButtonText: '确定',
      });
    }
  },
};
</script>
<style>
.reply-cell {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  /*max-height: 30px;*/
  line-height: 70px;
}
.el-table .cell {
  height: 60px;
  overflow: hidden;
  line-height: 70px;
}

.page {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.responseText {
  position: absolute;
  top: 50%;
  left: 650px;

  background-color: rgb(70, 175, 206);
  display: flex;
  align-items: center;
  justify-content: center;
}
.has-gutter .cell{
  color: #419dbf;
}
.el-pagination.is-background .el-pager li:not(.disabled):hover{
  color: #419dbf;
}
.el-pagination.is-background .el-pager li:not(.disabled).active{
  background-color: #419dbf;
}
.el-button--primary{
  background-color: #419dbf;
  border-color: #419dbf;
}
.el-button--primary:focus{
  background-color: #419dbf;
  border-color: #419dbf;
}
.el-button--primary:hover{
  background-color: #3885a1;
  border-color: #3885a1;
}
.goodRes{
  background-color: #419dbf;
  border-color: #419dbf;
  color: white;
  position: absolute;
  left: 1280px;
  top: 70px;
}
.goodRes:focus{
  background-color: #419dbf;
  border-color: #419dbf;
  color: white;
}
.goodRes:hover{
  background-color: #3885a1;
  border-color: #3885a1;
  color: white;
}
</style>
